const userService = require("../../services/userService");
import Dialog from "../../vant/dialog/dialog";

var app = getApp();
Page({
  data: {
    assets: app.assets,
    assetsByEnv: app.assetsByEnv,
    list: [],
    topList:[],
    articleList: [],
    num1: 6,
    num2: 523421,
    num3: 719433,
    timeClear: "",
    user: null,
    popup: false,
    showLogin: false,
    showLogin1: false,
  },

  onShareAppMessage: function () {
    return {
      imageUrl: this.data.assetsByEnv + "/tanchuang1.png",
      title: "鞋库鉴定",
      path: "/pages/home/home",
      success: function (res) {},
      fail: function (res) {},
    };
  },
  // onPullDownRefresh: function() {
  //     var user = wx.getStorageSync('user');
  //     wx.showToast({
  //         title: '下拉刷新中...',
  //         icon:'loading'
  //     });
  //
  //     if (user) {
  //         this.setData({
  //             user:user
  //         });
  //         this._getAppraList();
  //     }
  // },
  onShow: function () {
    var user = wx.getStorageSync("user");
    if (user) {
      // 安装微信双开会共享缓存并丢失部分信息
      if (!user.openid) {
        wx.clearStorage();
        return;
      }
      this.setData({
        user: user,
      });
      this._getAppraList();
    }
    this._getAllArticle();
  },
  randNum: function (m, n) {
    var num = Math.floor(Math.random() * (m - n) + n);
    return num;
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: "鞋库鉴定",
    });
    var that = this;
    var num1 = this.randNum(3, 6);
    var num2 = this.randNum(523421, 523489);
    var num3 = this.randNum(719433, 719533);

    this.setData({
      num1: num1,
      num2: num2,
      num3: num3,
    });
    setInterval(function () {
      var numRan = that.randNum(4, 6);
      that.setData({
        num1: numRan,
      });
    }, 3000);
  },
  bindGetUserInfo(e) {
    var user = wx.getStorageSync("user");
    if (!user) {
      //调用应用实例的方法获取全局数据
      if (e.detail.errMsg == "getUserInfo:ok") {
        app.getUserInfo(e.detail, function (userInfo) {
          wx.reLaunch({
            url: "../index/index",
          });
        });
      } else {
        this.setData({
          isTriggerAtuh: false,
        });
      }
    } else {
      var hasStatus0 = this.data.list.filter((item) => item.order.status == 0);
      if (hasStatus0 && hasStatus0.length > 0) {
        this.setData({
          popup: true,
        });
      } else {
        wx.navigateTo({
          url: "../appra/appra",
        });
      }
    }
  },
  detailRemark(e) {
    Dialog.alert({
      confirmButtonText: "关闭",
      title: "补图原因",
      message: e.target.dataset.remark,
    }).then(() => {
      // on close
    });
  },
  closePopup() {
    this.setData({
      popup: false,
    });
  },
  goAppra() {
    this.setData({
      popup: false,
    });
    wx.navigateTo({
      url: "../appra/appra",
    });
  },

  handlerLogin(e) {
    var user = wx.getStorageSync("user");
    if (!user) {
      this.setData({
        showLogin: true,
      });
    } else {
      var hasStatus0 = this.data.list.filter((item) => item.order.status == 0);
      if (hasStatus0 && hasStatus0.length > 0) {
        this.setData({
          popup: true,
        });
      } else {
        wx.navigateTo({
          url: "../appra/appra",
        });
      }
    }
  },
  handlerLogin1(e) {
    var user = wx.getStorageSync("user");
    if (!user) {
      this.setData({
        showLogin1: true,
      });
    } else {
      this.goList();
    }
  },
  bindGetUserInfo1(e) {
    var that = this;
    var user = wx.getStorageSync("user");
    if (!user) {
      //调用应用实例的方法获取全局数据
      if (e.detail.errMsg == "getUserInfo:ok") {
        app.getUserInfo(e.detail, function (userInfo) {
          that.goList();
          // wx.reLaunch({
          //     url: '../index/index'
          // })
        });
      } else {
        this.setData({
          isTriggerAtuh: false,
        });
      }
    } else {
      that.goList();
    }
  },
  bindGetUserInfo2(e) {
    var that = this;
    var user = wx.getStorageSync("user");
    if (!user) {
      //调用应用实例的方法获取全局数据
      if (e.detail.errMsg == "getUserInfo:ok") {
        app.getUserInfo(e.detail, function (userInfo) {
          wx.reLaunch({
            url: "../index/index",
          });
        });
      } else {
        this.setData({
          isTriggerAtuh: false,
        });
      }
    }
  },
  goList() {
    wx.navigateTo({
      url: "../appraList/appraList",
    });
  },
  goDetail(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../appraDetail/appraDetail?id=" + id,
    });
  },
  goArticleDetail(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../articleDetail/articleDetail?id=" + id,
    });
  },
  _getAppraList: function () {
    var user = wx.getStorageSync("user");
    var openid = user.openid;
    var params = {
      customerId: openid,
    };
    userService.pageList(params).then(
      (data) => {
        if (data.result) {
          this.setData({
            list: data.obj.list,
          });
        } else {
          wx.showToast({ title: data.errorMsg, icon: "none" });
        }
      },
      (error) => {
        wx.showToast({ title: "addAppra接口异常", icon: "none" });
      }
    );
  },
  _getAllArticle: function () {
    userService.getAllArticle({}).then(
      (data) => {
        if (data.result) {
          let topList = data.obj.filter((item) => {
            return item.added === 1
          })
          let articleList = data.obj.filter((item) => {
            return item.added !== 1
          })
          this.setData({
            topList:topList,
            articleList: articleList
          });
        } else {
          wx.showToast({ title: data.errorMsg, icon: "none" });
        }
      },
      (error) => {
        console.log(error);
        wx.showToast({ title: "getAllArticle接口异常", icon: "none" });
      }
    );
  },
});
